package com.one.utils;

import cn.hutool.core.io.IoUtil;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

import java.io.File;
import java.io.IOException;
import java.io.Writer;

public class WordUtil {
    public static final String DOCXFILEPEX="空气质量监测报告.docx";
    public static final String TEMPLATE="空气质量监测报告.ftl";
    public static final String WINDOWSDOCUMENT="C:/airQuality";
    /**
     * 生成Word
     * @param templateDir   模板所在的目录
     * @param templateName  模板文件名称
     * @param writer        输出流
     * @param dataModel     模板参数数据
     */
    public static void generateWord(File templateDir, String templateName, Writer writer, Object dataModel) {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
        configuration.setDefaultEncoding("UTF-8");
        try {
            configuration.setDirectoryForTemplateLoading(templateDir);
            Template template = configuration.getTemplate(templateName);
            template.process(dataModel, writer);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(writer);
        }
    }
}
